//
//  QQController.m
//  设置导航栏透明度和颜色
//
//  Created by zxy on 2018/6/13.
//  Copyright © 2018年 zxy. All rights reserved.
//

#import "QQController.h"
#import "WRNavigationBar.h"
#import "TestController.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define NAV_HEIGHT ((kScreenHeight == 812.0) ? 88 : 64)
#define ImageHeigh 260
#define Max_Scrol_offset 80
#define Max_offset_Y -(ImageHeigh + Max_Scrol_offset)

//透明度是否完全显示零界点,当滑动距离>=-60时，透明度为1，否则为0
#define NAVBAR_COLORCHANGE_POINT -60

@interface QQController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic,strong)UIImageView *headerImageView;
@end

@implementation QQController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"类似QQ空间效果";
    self.tableView.contentInset = UIEdgeInsetsMake(ImageHeigh - NAV_HEIGHT, 0, 0, 0);
    [self.tableView addSubview:self.headerImageView];
    [self.view addSubview:self.tableView];
    
    [self wr_setNavBarBackgroundAlpha:0];
}
#pragma mark - getter / setter
- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(UIImageView *)headerImageView{
    if(!_headerImageView){
        _headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -ImageHeigh, kScreenWidth, ImageHeigh)];
        _headerImageView.image = [UIImage imageNamed:@"image3"];
        _headerImageView.contentMode = UIViewContentModeScaleAspectFill;
        _headerImageView.clipsToBounds = YES;
    }
    return _headerImageView;
}

#pragma mark - tableview delegate / dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                   reuseIdentifier:nil];
    NSString *str = [NSString stringWithFormat:@"WRNavigationBar %zd",indexPath.row];
    cell.textLabel.text = str;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TestController *ctl = [[TestController alloc]init];
    [self.navigationController pushViewController:ctl animated:YES];
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = scrollView.contentOffset.y;
    if(offsetY > NAVBAR_COLORCHANGE_POINT){//上拉,完全显示
        [self changeNavBarAnimateWithIsClear:NO];
    }else{
        [self changeNavBarAnimateWithIsClear:YES];
    }
    
    //限制下拉偏移量
    if(offsetY < Max_offset_Y){
        [scrollView setContentOffset:CGPointMake(0, Max_offset_Y)];
    }
    // 改变图片框的大小 (上滑的时候不改变)
    // 这里不能使用offsetY，因为当（offsetY < LIMIT_OFFSET_Y）的时候，y = LIMIT_OFFSET_Y 不等于 offsetY
    CGFloat newOffset = scrollView.contentOffset.y;
    if(newOffset < -ImageHeigh){
        self.headerImageView.frame = CGRectMake(0, newOffset, kScreenWidth,-newOffset);
    }
}

- (void)changeNavBarAnimateWithIsClear:(BOOL)isClear
{
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.6 animations:^
     {
         __strong typeof(self) pThis = weakSelf;
         if (isClear == YES) {
             [pThis wr_setNavBarBackgroundAlpha:0];
         } else {
             [pThis wr_setNavBarBackgroundAlpha:1.0];
         }
     }];
}

@end
